package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AuthDetailEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.self.TeamAuthEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.authentication.TeamAuthActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TeamAuthModel extends BaseViewModel {
    public ObservableField<TeamAuthEntity> teamAuthEntity = new ObservableField<>(new TeamAuthEntity());

    public ObservableField<TeamAuthEntity.RealNameUserDTO> person = new ObservableField<>(new TeamAuthEntity.RealNameUserDTO());

    private TeamAuthActivity activity;
    private SelfRepertory repertory;

    public TeamAuthModel(TeamAuthActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //团队认证
    public void teamCertification() {
        DialogHelper.showProgressDialog(activity, null, "发布中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().teamCertification(teamAuthEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.authStatus();
                } else if (response.body().getMsg() != null) {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(activity, "发送服务器失败");
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //经营类型
    public void getTeamManagementType() {
//        DialogHelper.showProgressDialog(activity, null, "发布中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getUserAPI().getTeamManagementType().enqueue(new Callback<BaseEntity<List<AuthTeamType>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<AuthTeamType>>> call, Response<BaseEntity<List<AuthTeamType>>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() != null && response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                    activity.showManageTypePop(response.body().getData());
//                } else if (response.body().getMsg() != null) {
//                    MToast.showToast(activity, response.body().getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<AuthTeamType>>> call, Throwable t) {
//                MToast.showToast(activity, "发送服务器失败");
//                DialogHelper.dismissProgressDialog();
//            }
//        });
        APIManager.getInstance().getBusinessManagementAPI().getNewServiceType(2).enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>> data) {
                if (data != null && data.getData() != null){
                    activity.showManageTypePop(data.getData());
                }

            }
        });
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body()==null) {
                    MToast.showToast(activity, "网络连接超时，请检查网络");
                    return;
                }
                if (response.body().getCode().equals("200"))
                    activity.showAddressPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void getCodeModel(String mobile) {
        APIManager.getInstance().getUserAPI().sendSMS(mobile).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() != null)
                    MToast.showToast(activity, response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void getUserInfo(String userNo) {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamAuthDetail1(userNo).enqueue(new Callback<BaseEntity<TeamAuthEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TeamAuthEntity>> call, Response<BaseEntity<TeamAuthEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    teamAuthEntity.set(response.body().getData());
                    activity.showDetail(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TeamAuthEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
